<?php 
/*
 * #文件预览-输出控制器
 */
class Document extends CI_Controller{
	function Document(){
		parent::__construct();
	}
	function preview($fid){
		$this->load->model('share/File');
		$path = $this->db->query("SELECT file_path FROM dy_upload WHERE fid = $fid");
		$right_path = $path->result_array();
		if($right_path){	
			$this->load->helper('file');
			$result = read_file($right_path[0]['file_path']);
			
			$information = $this->File->get($fid);
			$name = $information['title'];
			$right_rank = $information['rank'];
			$right_rank_times = $information['rank_times'];
			$author = $this->user_model->get_user_name($information['uid']);
			$time = $information['upload_time'];
			$download_times = $information['download_times'];
			$subject = $information['subject'];
			$year = $information['year'];
			$category = $information['category'];
			$description = $information['description'];
			$tag = $information['tag'];
			$uid = $information['uid'];
			$swf_path = $information['swf_path'];
			$other_share_comments = $this->File->get_all_by_category($category);
			
			
			//评论
			$this->load->model("share/comment");
			$this->load->model("user/user_model");			
			$com_arr = $this->comment->get_all($fid);
			
			foreach ($com_arr as $key => $val)
			{
				$com_arr[$key]["username"] = $this->user_model->get_user_name($val["uid"]);
			}
			if($right_rank_times == 0)
				$right_rank = 0;
			else
				$right_rank = $right_rank / $right_rank_times;
			
			$data = array(
			'fid' => $fid,
			'uid' => $uid,
			'swf_path' => $swf_path,
			'name' => $name,
			'right_rank' => $right_rank,
			'right_rank_times' => $right_rank_times,
			'author' => $author,
			'time' => $time,
			'other_share_comments' => $other_share_comments,
			'download_times' => $download_times,
			'subject' => $subject,
			'year' => $year,
			'category' => $category,
			'tag' => $tag,
			'description' => $description,
			'comment' => $com_arr
			);
			$this->parser->parse('header',array('title'=>"文件预览 - $name"));
			$this->load->view('share/preview',$data);
			$this->parser->parse('footer',array());
		}
		else{
			show_error("没有这个分享！");
		}
		//return $data;
	}
	function file_download($fid){
		$uid = $this->user_model->get_uid();
		if($uid == 0)
		{
			redirect(base_url()."tipask/?user/login.html");
			exit();
		}
 
		$this->load->model('share/File');
		$this->load->model('share/File');
		$this->File->add_download_times($fid);
		$path = $this->db->query("SELECT file_path FROM dy_upload WHERE fid = $fid");
		$right_path = $path->result_array();
		
		$this->load->helper('download');
		$this->load->helper('file');
		$result = read_file($right_path[0]['file_path']);
		$name = $this->db->query("SELECT file_name FROM dy_upload WHERE fid = $fid");
		$right_name = $name->result_array();
		force_download($right_name[0]['file_name'],$result);
	}
	function delete($unknown_user_uid,$fid){
		$this->load->helper('file');
		$this->load->model('share/File');
		
		$information = $this->File->get($fid);
		$right_path = $information['file_path'];
		$uid = $information['uid'];
		
		if($unknown_user_uid == $uid){
			unlink($right_path);
			$this->File->delete($fid);
			return TRUE;
		}
		return FALSE;
	}
	function index(){
		$data = Document::preview(25);
		$this->load->view('share/preview1',$data);
	}
}
?>